#!/bin/csh -f
# jfd@Tue Dec 19 13:37:52 CST 2000
#
# Shell script to analyse the violations of NOE distance
# restraints, using Aqua modules.
# The results will be shown as links in the overview file.
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv != 7 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
    echo 'usage: '
    echo 'viol_chk.csh  dir_numb           pdb_type        external_match'
    echo '              multi_restraints   averaging_type  number_monomers   terseness'
    echo ''
    echo '   dir_numb:           number of directory at the server side'
    echo '   terseness:          0 through 9'
    echo '   pdb_type:           -pdb, -xplor, or -molmol'
    echo '   external_match:     true, or false'
    echo '   multi_restraints:   true, or false'
    echo '   averaging_type:     standard, r6ave, or r6sum'
    echo '   number_monomers:    1 through 999'

    goto ending


#####################
# initialization
#####################
begin:
# SAME AS IN PERL SCRIPTS, MANY ARE REDUNDANT TO THIS PROGRAM

# SITE DEPENDANT
    set serv_loc        = "AQUA_LOC/Aqua/server/"
    set serv_dir        = "AQUA_DIR/Aqua/server/"

    set cgi_bin_loc     = "CGI_LOC/Aqua/"
    set cgi_bin_dir     = "CGI_DIR/Aqua/"

    set tmp_loc         = "TMP_LOC/Aqua/"
    set tmp_dir         = "TMP_DIR/Aqua/"

    set webmaster       = "WEBMASTER"

# SITE INDEPENDENT
    set author          = "jurgen\@bmrb.wisc.edu"
    set overview        = "overview.html"
    set debug           = "false"
    set scriptlogfile   = "script.log"
    
    setenv aquaroot $serv_dir/..
    if ( $debug == "true" ) then
        echo 'Aqua program directory:' $aquaroot
    endif
    source $aquaroot/aqsetup
    
    umask 0000              # File creation mask
    limit cputime     60    # Maximum number of seconds the CPU can spend
                            # on any single process spawned.
                            # This should be half the $cal_time_secs
                            # defined in the calling cgi script.
    limit filesize   100m   # Maximum size of any one file
    limit datasize   100m   # Maximum size of data (including stack)
    limit coredumpsize 0    # Maximum size of core dump file
    
    set path = ($path /usr/share/bin /usr/sbin \
                  $cgi_bin_dir )
# For Utrecht locations of ?
    set path = ( /usr/ruuci11/people/ruu/bin $path )
        

#####################
# process the options
#####################
options:
switch ( "$2" )
    case "-pdb":
        set pdbtype = ""
        breaksw
    case "-xplor":
        set pdbtype = "-xplor"
        breaksw
    case "-molmol":
        set pdbtype = "-molmol"
        breaksw
    default:
        goto usage
endsw

switch ( "$3" )
    case "true":
        set external_match = "-ext"
        breaksw
    case "false":
        set external_match = ""
        breaksw
    default:
        goto usage
endsw

switch ( "$4" )
    case "true":
        set multi_restraints = ""
        breaksw
    case "false":
        set multi_restraints = "-multi"
        breaksw
    default:
        goto usage
endsw

switch ( "$5" )
    case "standard":
        set averaging_type = ""
        breaksw
    case "r6ave":
        set averaging_type = "-r6ave"
        breaksw
    case "r6sum":
        set number_monomers = "$6"
        set averaging_type = "-r6sum "$number_monomers
        breaksw
    default:
        goto usage
endsw

set terseness = "$7"
    
# go to the right dir

    set dir = $tmp_dir/$1

    if ( !(-e $dir ) )then
        echo "Directory $dir doesn't exist"
        goto ending
    endif
    
    cd $dir

# note our existence
    echo 'DETAILS CAN BE FOUND IN THE LOG FILE: aqpc.log'
    echo ' '
    echo 'VIOLCHK - Analyzing the violation of NOE restraints'  >> $scriptlogfile

# set coordinates file
    set coords  = "struct"
    
# setup project name
    set proj = `qgetproj $coords`
    if ( $debug == "true" ) then
        echo 'VIOLCHK - Project name is' $proj    
    endif
    qproject $proj

# convert uploaded dos files to unix. Macs might go wrong here?
# Yes they do, see email discussion with Steve Alam who was trying to use
# the web server with a Mac OS.
    gawk '{gsub (/\015/,"",$0);print}' $proj.pdb > $proj.pdb.tmp
    \mv $proj.pdb.tmp $proj.pdb
    gawk '{gsub (/\015/,"",$0);print}' $proj.noe > $proj.noe.tmp
    \mv $proj.noe.tmp $proj.noe
    
# prepare for violation check, and run violchk

    echo 'VIOLCHK - Running violchk'                            >> $scriptlogfile    
    
    if ( $debug == "true" ) then
        echo 'executing:'
        echo "\
    aqpc -terseness $terseness\
        $pdbtype $external_match $multi_restraints \
        $averaging_type \
        $coords.pdb"
    endif
    
    aqpc -terseness $terseness\
        $pdbtype $external_match $multi_restraints \
        $averaging_type \
        $coords.pdb

# prepare a figure per residue
        
    if ( !(-e struct.nrv) ) then
        echo 'VIOLCHK - ERROR no resulting nrv file created'    >> $scriptlogfile
        goto ending
    else
        set result = "true"    
    endif

ending:
#   Update the result file

    if ( ! $?result ) then
        set result = "false"
    endif
        
    if ( $result == "true") then    
        set block = "RESTRAINT_VIOLATION_SUMMARY_OVERALL"
        set item  = "MaxUPPv"
        set violation = (`qdbext_non_int -v b=$block -v i=$item struct.nrv`)
    else
        set violation = "Not_a_Number"
    endif

    echo 'VIOLCHK - Update the result file in dir' $1           >> $scriptlogfile
    echo 'VIOLCHK - result is                    ' $result      >> $scriptlogfile
    echo 'VIOLCHK - violation is                 ' $violation   >> $scriptlogfile

    echo 'Finished violation check'                             >> $scriptlogfile
    
    touch finished # This file will be checked for by the perl script
    
    exit(0)

